import 'package:device_info_plus/device_info_plus.dart';
import 'dart:io';
import 'package:jm_flutter_common/jm_flutter_common.dart';

class MyDeviceInfo {
  static const tag = "MyDeviceInfo";
  static final DeviceInfoPlugin _deviceInfoPlugin = DeviceInfoPlugin();
  static bool _isInitialized = false;
  static AndroidDeviceInfo? _androidInfo;
  static IosDeviceInfo? _iosInfo;
  static WebBrowserInfo? _webBrowserInfo;
  static WindowsDeviceInfo? _windowsInfo;
  static LinuxDeviceInfo? _linuxInfo;
  static MacOsDeviceInfo? _macOsInfo;

  // 私有构造函数，防止实例化
  MyDeviceInfo._();

  static Future<void> init() async {
    if (_isInitialized) {
      return;
    }
    // 模拟初始化逻辑
    _isInitialized = true;
    switch (Platform.operatingSystem) {
      case "android":
        _androidInfo = await _deviceInfoPlugin.androidInfo;
        break;
      case "ios":
        _iosInfo = await _deviceInfoPlugin.iosInfo;
        break;
      case "web":
        _webBrowserInfo = await _deviceInfoPlugin.webBrowserInfo;
        break;
      case "windows":
        _windowsInfo = await _deviceInfoPlugin.windowsInfo;
        break;
      case "linux":
        _linuxInfo = await _deviceInfoPlugin.linuxInfo;
        break;
      case "macos":
        _macOsInfo = await _deviceInfoPlugin.macOsInfo;
        break;
    }

    logd(tag,
        "init deviceName:$deviceName, model:$model, iosIdentifierToName:${iosIdentifierToName(model)}, systemVersion: $systemVersion");
    if (kIsAndroid) {
      logd(tag,
          "brand: ${_androidInfo!.brand}, manufacturer: $manufacturer, hardware: $hardware, sdkInt: $androidSdkInt");
    }
  }

  static String get deviceName {
    if (_androidInfo != null) {
      return _androidInfo!.model;
    } else if (_iosInfo != null) {
      return _iosInfo!.name;
    } else if (_webBrowserInfo != null) {
      return _webBrowserInfo!.browserName.toString();
    } else if (_windowsInfo != null) {
      return _windowsInfo!.computerName;
    } else if (_linuxInfo != null) {
      return _linuxInfo!.name;
    } else if (_macOsInfo != null) {
      return _macOsInfo!.model;
    } else {
      return "Unknown";
    }
  }

  static String get model {
    if (_androidInfo != null) {
      return _androidInfo!.model;
    } else if (_iosInfo != null) {
      return _iosInfo!.utsname.machine;
    } else if (_webBrowserInfo != null) {
      return _webBrowserInfo!.browserName.toString();
    } else if (_windowsInfo != null) {
      return _windowsInfo!.computerName;
    } else if (_linuxInfo != null) {
      return _linuxInfo!.name;
    } else if (_macOsInfo != null) {
      return _macOsInfo!.model;
    } else {
      return "Unknown";
    }
  }

  static String get brand {
    if (_androidInfo != null) {
      return _androidInfo!.brand;
    } else if (_iosInfo != null) {
      return _iosInfo!.name;
    } else {
      return "Unknown";
    }
  }

  static String get manufacturer {
    if (_androidInfo != null) {
      return _androidInfo!.manufacturer;
    }
    return "Unknown";
  }

  static String get hardware {
    if (_androidInfo != null) {
      return _androidInfo!.hardware.toLowerCase();
    }
    return "Unknown";
  }

  static String get systemVersion {
    if (_androidInfo != null) {
      return _androidInfo!.version.release;
    } else if (_iosInfo != null) {
      return _iosInfo!.systemVersion;
    }
    return "0";
  }

  static int get androidSdkInt {
    if (_androidInfo != null) {
      return _androidInfo!.version.sdkInt;
    }
    return 0;
  }

  static bool get isIpad {
    if (_iosInfo != null) {
      return _iosInfo!.utsname.machine.startsWith("iPad");
    }
    return false;
  }

  static bool get isIphone {
    if (_iosInfo != null) {
      return _iosInfo!.utsname.machine.startsWith("iPhone");
    }
    return false;
  }

  static double get iosModelValue {
    if (_iosInfo != null) {
      final identifier = _iosInfo!.utsname.machine;

      if (identifier.startsWith("iPhone")) {
        final valueStr = identifier.substring(6).replaceAll(",", ".");
        final value = double.tryParse(valueStr);
        if (value != null) {
          return value;
        }
      } else if (identifier.startsWith("iPad")) {
        final valueStr = identifier.substring(4).replaceAll(",", ".");
        final value = double.tryParse(valueStr);
        if (value != null) {
          return value;
        }
      }
    }
    return 0;
  }

  // --- 关键步骤：将标识符映射到型号 ---
  static String iosIdentifierToName(String identifier) {
    // 这个映射表需要你手动维护，或者查找现有的库/资源
    // 以下仅为示例，不完整，请根据需要扩展
    // 参考资源: https://www.theiphonewiki.com/wiki/Models
    switch (identifier) {
      // iPhone
      case 'iPhone1,1':
        return 'iPhone';
      case 'iPhone1,2':
        return 'iPhone 3G';
      case 'iPhone2,1':
        return 'iPhone 3GS';
      case 'iPhone3,1':
      case 'iPhone3,2':
      case 'iPhone3,3':
        return 'iPhone 4';
      case 'iPhone4,1':
        return 'iPhone 4S';
      case 'iPhone5,1':
      case 'iPhone5,2':
        return 'iPhone 5';
      case 'iPhone5,3':
      case 'iPhone5,4':
        return 'iPhone 5c';
      case 'iPhone6,1':
      case 'iPhone6,2':
        return 'iPhone 5s';
      case 'iPhone7,2':
        return 'iPhone 6';
      case 'iPhone7,1':
        return 'iPhone 6 Plus';
      case 'iPhone8,1':
        return 'iPhone 6s';
      case 'iPhone8,2':
        return 'iPhone 6s Plus';
      case 'iPhone8,4':
        return 'iPhone SE (1st generation)';
      case 'iPhone9,1':
      case 'iPhone9,3':
        return 'iPhone 7';
      case 'iPhone9,2':
      case 'iPhone9,4':
        return 'iPhone 7 Plus';
      case 'iPhone10,1':
      case 'iPhone10,4':
        return 'iPhone 8';
      case 'iPhone10,2':
      case 'iPhone10,5':
        return 'iPhone 8 Plus';
      case 'iPhone10,3':
      case 'iPhone10,6':
        return 'iPhone X';
      case 'iPhone11,2':
        return 'iPhone XS';
      case 'iPhone11,4':
      case 'iPhone11,6':
        return 'iPhone XS Max';
      case 'iPhone11,8':
        return 'iPhone XR';
      case 'iPhone12,1':
        return 'iPhone 11';
      case 'iPhone12,3':
        return 'iPhone 11 Pro';
      case 'iPhone12,5':
        return 'iPhone 11 Pro Max';
      case 'iPhone12,8':
        return 'iPhone SE (2nd generation)';
      case 'iPhone13,1':
        return 'iPhone 12 mini';
      case 'iPhone13,2':
        return 'iPhone 12';
      case 'iPhone13,3':
        return 'iPhone 12 Pro';
      case 'iPhone13,4':
        return 'iPhone 12 Pro Max';
      case 'iPhone14,4':
        return 'iPhone 13 mini';
      case 'iPhone14,5':
        return 'iPhone 13';
      case 'iPhone14,2':
        return 'iPhone 13 Pro';
      case 'iPhone14,3':
        return 'iPhone 13 Pro Max';
      case 'iPhone14,6':
        return 'iPhone SE (3rd generation)';
      case 'iPhone14,7':
        return 'iPhone 14';
      case 'iPhone14,8':
        return 'iPhone 14 Plus';
      case 'iPhone15,2':
        return 'iPhone 14 Pro';
      case 'iPhone15,3':
        return 'iPhone 14 Pro Max';
      case 'iPhone15,4':
        return 'iPhone 15';
      case 'iPhone15,5':
        return 'iPhone 15 Plus';
      case 'iPhone16,1':
        return 'iPhone 15 Pro';
      case 'iPhone16,2':
        return 'iPhone 15 Pro Max';
      case 'iPhone17,2':
        return 'iPhone 16 Pro Max';

      // iPad ... (需要添加 iPad 的映射)
      case 'iPad1,1':
        return 'iPad';
      // ...等等

      // iPod touch ... (需要添加 iPod 的映射)

      // 模拟器
      case 'i386':
      case 'x86_64':
      case 'arm64':
        return 'Simulator'; // 模拟器通常是这些标识符

      default:
        return identifier; // 如果没匹配到，直接返回原始标识符
    }
  }
}
